﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json.Converters;
using DotNet.Common.Command;
using Newtonsoft.Json;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Web.ZLOAuth.Code;
using ZLCMS.Data.Model;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.ZLOAuth
{
    public partial class CommandServices : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IsoDateTimeConverter convert = new IsoDateTimeConverter();
            convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

            object result = new CommandResult<object>() { ErrorCode = "0001", Result = "请根据规定调用接口" };

            //验证签名
            string data = "key=" + Request["key"] + "&param=" + Request["param"] + "&private=" + OAuthServerConfig.PrivateKey;
            string sign =
                 System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(data, "MD5").ToLower(); 
            if (sign != Request["sign"])
            {
                result = new CommandResult<object>() { ErrorCode = "0001", Result = "验证签名失败" };
            }
            else if (!string.IsNullOrEmpty(Request["key"]))
            {
                result = new CommandExecutor().Execute(Request["key"], Request["param"]);
            }
            
            string resultStr = JsonConvert.SerializeObject(result, convert);

            //CMSKits.WriteLog(Request["key"], Session[GlobalSessionString.CurrentMemberAccount] as string, Request["param"] + "=====>>>>" + resultStr);

            Response.Write(resultStr);
        }
    }
}